package com.app.service.algs;

import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.tzbb.*;
import com.app.service.UploadParameterService;
import com.app.service.YjDataService;
import com.app.utils.MathUtil;
import com.app.utils.TimeHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
@Slf4j
public class TzbbUploadService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;

    public Uploadresultdata getTzbbData(List<YjData> yjDataList, UploadParameter uploadParameter, Accountverify account) {
        Uploadresultdata resultdata = new Uploadresultdata();
        resultdata.setSBeamCode(yjDataList.get(0).getBridge_code());
        resultdata.setNProjectID(account.getNProjectID());
        resultdata.setNSectionID(account.getNSectionID());
        resultdata.setSSitesName(uploadParameter.getEquipmentname());
        resultdata.setSPlaceName(uploadParameter.getGcmc());
        resultdata.setSProPlace(uploadParameter.getSgbw());
        resultdata.setSBeamType(uploadParameter.getLblx());
        resultdata.setNBeamLength(Float.valueOf(uploadParameter.getSgdw()));
        resultdata.setNHoleNum(Integer.valueOf(uploadParameter.getKongdaoshu()));
        resultdata.setDCastingDate(TimeHelper.addDay(yjDataList.get(0).getMudjack_date(),-7));
        resultdata.setNGroutNum(yjDataList.size());
        resultdata.setSStepName(uploadParameter.getYajiangbuzhou());
        resultdata.setDGroutingDate(yjDataList.get(0).getMudjack_date() + " " + yjDataList.get(0).getBegin_time());
        String deviceCode = yjDataList.get(0).getDevice_code();
        resultdata.setSGroDevCode(deviceCode);
        resultdata.setSCementGrade(uploadParameter.getSnmc());
        resultdata.setSGroAgenName(uploadParameter.getYajiangji());
        resultdata.setSGroAspect(uploadParameter.getYajiangfangxiang());
        resultdata.setNStirTime(Float.valueOf(uploadParameter.getStirtime()));
        resultdata.setNInitialFluidity(Float.valueOf(uploadParameter.getMsl()));
        resultdata.setNSlurryTemperature(yjDataList.get(0).getSlurry_temperature());
        resultdata.setNEhsTemperature(yjDataList.get(0).getEnvironment_temperature());
        resultdata.setSMixtureProportion(uploadParameter.getPhb());
        if (deviceCode.equals("2023091801")) {
            resultdata.setSConOperator("张德恒");
            resultdata.setSSupervisors("杨玉平");
            resultdata.setSQuaInspector("朱仙学");
            resultdata.setSManufacturers("开封齐力");
        } else {
            resultdata.setSConOperator("/");
            resultdata.setSSupervisors("/");
            resultdata.setSQuaInspector("/");
            resultdata.setSManufacturers("/");
        }
        List<BeamGroutResData> beamGroutResDataList = new ArrayList<>();
        for (int i = 0; i < yjDataList.size(); i++) {
            BeamGroutResData beamGroutResData = new BeamGroutResData();
            beamGroutResData.setNIndex(i + 1);
            beamGroutResData.setSSteelCode(yjDataList.get(i).getDuct_code());
            beamGroutResData.setTGrouBeginTime(yjDataList.get(i).getBegin_time());
            beamGroutResData.setTGrouEndTime(yjDataList.get(i).getEnd_time());
            beamGroutResData.setNInSlurryPressure(yjDataList.get(i).getMudjack_pressure());
            beamGroutResData.setNAntiSlurryPressure(yjDataList.get(i).getStabilize_pressure());
            beamGroutResData.setNPressureTime(Float.valueOf(yjDataList.get(i).getPressurize_time()));
            beamGroutResData.setNWaterBinderRatio(yjDataList.get(i).getWater_binder_ratio());
            beamGroutResData.setNtheoryGroutingquantity(yjDataList.get(i).getTheory_volume());
            beamGroutResData.setNrealityGroutingquantity(yjDataList.get(i).getTheory_volume());
            beamGroutResData.setNGroutingquantityerror(0f);
            beamGroutResData.setSRemarks("无");
            beamGroutResDataList.add(beamGroutResData);
        }
        resultdata.setBeamGroutResData(beamGroutResDataList);
        return resultdata;
    }

    /**
     * 分割出字符串中的数字
     *
     * @param str 需要进行分割的字符串
     * @return 分割出的整数
     */
    public static int quString2(String str) {
//    	将指定字符串中的字符替换
//    	str.replaceAll(String regex, String replacement)
//      第一个参数是要替换掉str中的谁，第二个参数是要把第一个参数替换成啥。
        return Integer.parseInt(str.replaceAll("[^(0-9)]", ""));
    }

    public static void main(String[] args) {
        int n10 = quString2("N10");
        System.out.println(n10);
    }

    public List<Uploadrawdata> getDraw(Uploadresultdata tzbbData, List<YjData> yjDataList, DataResult dataResult) {
        List<Uploadrawdata> drawDataList = new ArrayList<>();
        for (int i = 0; i < yjDataList.size(); i++) {
            Uploadrawdata rawData = new Uploadrawdata();
            rawData.setNBeamID(dataResult.getNBeamID());
            rawData.setSGroutingDate(yjDataList.get(i).getMudjack_date());
            rawData.setSSteelCode(yjDataList.get(i).getDuct_code());
            rawData.setNGroOrder(i + 1);
            List<BeamGroRawData> rawDataList = new ArrayList<>();
            if ("2023052101".equals(yjDataList.get(i).getDevice_code())) {
                Map<String, List> graphData = yjDataService.getGraphData3(String.valueOf(yjDataList.get(i).getId()));
                List<String> dateList = graphData.get("dateList");
                List<Double> mcgList = graphData.get("mcgList");
                Map<String, List> graphData1 = yjDataService.getGraphData4(String.valueOf(yjDataList.get(i).getId()));
                List<Double> mcgList1 = graphData1.get("mcgList");
                for (int j = 0; j < mcgList.size(); j++) {
                    BeamGroRawData beamGroRawData = new BeamGroRawData();
                    beamGroRawData.setTime(dateList.get(j));
                    beamGroRawData.setIp(mcgList.get(j));
                    beamGroRawData.setOp(mcgList1.get(j));
                    beamGroRawData.setWaterbinderratio(tzbbData.getBeamGroutResData().get(0).getNWaterBinderRatio());
                    rawDataList.add(beamGroRawData);
                }
            } else if ("2023091801".equals(yjDataList.get(i).getDevice_code())) {
                Map<String, List> gasbGraphData = yjDataService.getGasbGraphData(String.valueOf(yjDataList.get(i).getId()));
                List<String> dateList = gasbGraphData.get("dateList");
                List<String> mcgList = gasbGraphData.get("mcgList");
                List<String> mcgOutList = gasbGraphData.get("mcgOutList");
                for (int j = 0; j < mcgList.size(); j++) {
                    BeamGroRawData beamGroRawData = new BeamGroRawData();
                    beamGroRawData.setTime(dateList.get(j));
                    beamGroRawData.setIp(Double.valueOf(mcgList.get(j)));
                    beamGroRawData.setOp(Double.valueOf(mcgOutList.get(j)));
                    beamGroRawData.setWaterbinderratio(tzbbData.getBeamGroutResData().get(0).getNWaterBinderRatio());
                    rawDataList.add(beamGroRawData);
                }
            }
            rawData.setBeamGroRawData(rawDataList);
            drawDataList.add(rawData);
        }

        return drawDataList;
    }

    public String save(String deviceCode, String bridgeCode, String ductCode, String mudjackDate, String beginTime, String endTime) {
        YjData yjData = new YjData();
        yjData.setBridge_code(bridgeCode);
        yjData.setDevice_code(deviceCode);
        yjData.setDuct_code(ductCode);
        yjData.setMudjack_date(mudjackDate);
        yjData.setBegin_time(beginTime);
        yjData.setEnd_time(endTime);
        yjData.setMudjack_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setSlurry_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setStabilize_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
        yjData.setPressurize_time("180");
        yjData.setWater_binder_ratio(0.28f);
        yjData.setTheory_volume(145f);
        yjData.setPractical_volume(120f);
        yjData.setPractical_mudjack_value(0f);
        yjData.setPractical_water_value(0f);
        yjData.setEnvironment_temperature(25.5f);
        yjData.setSlurry_temperature(19.2f);
        yjData.setPractical_fluidity("18");
        yjData.setRemark("");
        yjData.setMudjack_curve_graph("");
        yjData.setState("2");
        int save = yjDataMapper.save(yjData);
        String s = "";
        if (save > 0) {
            s = "保存成功";
        } else {
            s = "保存失败";
        }


        return s;
    }
}
